package game.tictactoe.pojo;

public abstract class Player 
{
	protected Seed mySeed;
	protected Seed oppSeed;
	
	public Player(Seed mySeed)
	{
		this.mySeed = mySeed;
		this.oppSeed = (mySeed == Seed.CROSS)? Seed.ZERO : Seed.CROSS;
	}
	

	public Seed getMySeed()
	{
		return mySeed;
	}

	public void setMySeed(Seed mySeed)
	{
		this.mySeed = mySeed;
	}

	public Seed getOppSeed()
	{
		return oppSeed;
	}

	public void setOppSeed(Seed oppSeed)
	{
		this.oppSeed = oppSeed;
	}
	
	private String[] getWinningPatterns(Seed seed)
	{
		String[] winningLocations = {"SSS******","***SSS***","******SSS","S**S**S**","*S**S**S*","**S**S**S","S***S***S","**S*S*S**"};

		String[] winningPatterns = new String[8];
		for(int i=0;i<winningLocations.length;i++)
		{
			String winningPattern = winningLocations[i];
			winningPattern =	winningPattern.replace("S", seed.toString());
			winningPatterns[i] = winningPattern;
		}
		return winningPatterns;
	}
	
	public boolean hasPlayerWon(Seed seed, Board board)
	{
		String[] winningPatternsForPlayer = getWinningPatterns(seed);
		String boardPatternForPlayer = board.toStringForPatternMatching(seed);
		for(String winningPatternForPlayer : winningPatternsForPlayer)
		{
			int count = 0;
			for(int i=0; i<winningPatternForPlayer.length(); i++)
			{
				char srcValue = winningPatternForPlayer.charAt(i);
				char destValue = boardPatternForPlayer.charAt(i);
				char seedValue = seed.getValue().charAt(0);
				if(srcValue == destValue  && destValue == seedValue)
				{
					count+=1;
				}
			}
			if(count >= 3)
				return true;
		}
		return false;
	}
	
	abstract public int getNextMove(Board board);
}
